package com.dyz.swordFingerOffer;

/**
 * @author: daiyizheng
 * @date: 2021/6/10 22:44
 * @description:
 */
public class ReverseSentence58 {
    public String ReverseSentence(String str) {
        //思路先整体全部反转
        //然后根据每个单词左反转
        String ans = new StringBuilder(str).reverse().toString();
        StringBuilder res = new StringBuilder();//接字符
        StringBuilder an = new StringBuilder(); //接单词
        for(int i= 0; i< ans.length(); i++){
            if(ans.charAt(i) == ' '){
                an.append(res.reverse().toString()).append(" ");
                res = new StringBuilder();
            }else {
                res.append(ans.charAt(i));
            }
        }
        //接最后一个单词
        an.append(res.reverse().toString()).append(" ");
        return an.toString().trim();

    }
}
